﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TruckWatch.Cloud.Models.EF;

namespace TruckWatch.Cloud.BusinessLogic
{
    public static class LocationBL
    {
        public static List<Location> GetAllLocations()
        {
            List<Location> ret = null;
            using (TruckWatchEntities context = new TruckWatchEntities())
            {
                ret = context.Locations.ToList();
            }
            return ret;
        }

        public static void UpldateLocation(Location location)
        {
            using (TruckWatchEntities context = new TruckWatchEntities())
            {
                Location existingLocation = context.Locations.First(x => x.Id == location.Id);

                existingLocation.AddressLine1 = location.AddressLine1;
                existingLocation.AddressLine2 = location.AddressLine2;
                existingLocation.City = location.City;
                existingLocation.State = location.State;
                existingLocation.Latitude = location.Latitude;
                existingLocation.Longitude = location.Longitude;
                existingLocation.LocationName = location.LocationName;
                existingLocation.ZipCode = location.ZipCode;
                
                context.SaveChanges();
            }
        }

        public static void AddLocation(Location location)
        {
            using (TruckWatchEntities context = new TruckWatchEntities())
            {
                context.AddToLocations(location);
                context.SaveChanges();
            }
        }

        public static void DeleteLocation(Location location)
        {
            using (TruckWatchEntities context = new TruckWatchEntities())
            {
                Location found = context.Locations.First(x => x.Id == location.Id);
                context.Locations.DeleteObject(found);
                context.SaveChanges();
            }
        }
    }
}
